package my.sa;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class AnnotionAnaly {

	int totalLines;
	int annotationLines;

	private String filePath;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String testPath = "E:/parttimework/毕业设计项目/Java静态分析/Test.java";
		AnnotionAnaly aa = new AnnotionAnaly(testPath);
		aa.analy();
		System.out.println(aa);

	}

	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("代码总行数：").append(totalLines).append("\r\n")
		.append("注释代码行数：").append(annotationLines).append("\r\n");
		return sb.toString();
	}
	
	public AnnotionAnaly(String filePath){
		this.filePath = filePath;
	}
	public void analy() {

		boolean isAnnotation2Begin = false;
		boolean isAnnotation1Begin = false;

		FileInputStream fin;
		try {
			fin = new FileInputStream(filePath);

			int c = -1;
			while ((c = fin.read()) > 0) {
				switch (c) {
				case '\n':
					totalLines++;
					if (isAnnotation2Begin) {
						isAnnotation2Begin = false;
						annotationLines++;
					}
					if (isAnnotation1Begin) {
						annotationLines++;
					}
					break;
				case '/':
					if ((c = fin.read()) > 0) {
						if (c == '/') {
							isAnnotation2Begin = true;
						} else if (c == '*') {
							isAnnotation1Begin = true;
						}
					}
					break;
				case '*':
					if((c = fin.read()) > 0){
						if(c=='/'){
							isAnnotation1Begin = false;
							annotationLines++;
						}
					}
					break;
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
